import {ajax,getUserInfo} from '@/common/common.js'
import {bpmUrl} from "@/common/config.js"
//初始赋值
function  formonload(list,formData,bussiness){
	let currentNodeid = bussiness.currentNodeid
	let arr = list.map(item => {
		if(formData.BudgetCode){
			if([
				'BudgetName',
				'CBZX',
				'BudgetItem',
				'MapBudget'
				].indexOf(item.name) != 1){
				item.disabled = true
			}
		}
		let WF_DocStatus = bussiness.docStatus
		if(WF_DocStatus=="Draft"){
		    if(formData.BudgetCode){
		        fznext1(formData);
		    }
		}
		return item
	})
	if(currentNodeid=="T10002"){
		// 初始化加载预算名称
		initListOption(list,'BudgetCode',bussiness);
	}
	return arr
}
// 初始化加载预算名称
function initListOption(list,name,bussiness){
	let Processid = bussiness.processid
	let user = getUserInfo()
	let paramas = {
		paramasMap: {
			Processid:Processid
		},
		headersMap: {
			sysid: "bpm",
			syspwd: "pass",
			userid: user.bpmUid
		},
		url: bpmUrl + '/rest/appform/appform/R_appform_CRMVR_B122'
	}
		ajax({
			url:'/bpm/get',
			method: 'POST',
			data: paramas,
			success:res => {
				let arr = res.data.data
				arr = arr.map(item => {
					item.label = item.text
					return item
				})
				list.map(item => {
					if(item.name == name){
						item.optiona.data = arr
					}
				})
			}
		})
}

//给预算号相关字段赋值
function fz1(item,formData,list){
    //获取当前选中的预算名称
    let BudgetCode = formData.BudgetCode
	let BudgetName = formData.BudgetName
	//预算号赋值
	let user = getUserInfo()
	let paramas = {
		paramasMap: {
			BudgetCode:BudgetCode,
			BudgetName:BudgetName
		},
		headersMap: {
			sysid: "bpm",
			syspwd: "pass",
			userid: user.bpmUid
		},
		url: bpmUrl + '/rest/appform/appform/R_appform_CRMVR_B123'
	}
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: paramas,
		success:res => {
			formData.BudgetName = res.data.data.BudgetName
		}
	})
    
    //成本中心赋值
	let CBZX = formData.CBZX
	paramas.paramasMap = {
		BudgetName:BudgetName,
		BudgetCode:BudgetCode,
		CBZX:CBZX
	}
	paramas.url = bpmUrl + '/rest/appform/appform/R_appform_CRMVR_B117'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: paramas,
		success:res => {
			formData.CBZX = res.data.data.CBZX
		}
	})
    //预算项目赋值
	let BudgetItem = formData.BudgetItem
	paramas.paramasMap = {
		BudgetName:BudgetName,
		BudgetCode:BudgetCode,
		BudgetItem:BudgetItem
	}
	paramas.url = bpmUrl + '/rest/appform/appform/R_appform_CRMVR_B118'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: paramas,
		success:res => {
			formData.BudgetItem = res.data.data.BudgetItem
		}
	})
    //预算号对应预算项目
	let MapBudget = formData.MapBudget
	paramas.paramasMap = {
		BudgetName:BudgetName,
		BudgetCode:BudgetCode,
		MapBudget:MapBudget
	}
	paramas.url = bpmUrl + '/rest/appform/appform/R_appform_CRMVR_B119'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: paramas,
		success:res => {
			formData.MapBudget = res.data.data.MapBudget
		}
	})
	
	list.map(item => {
		if(['BudgetName',
			'CBZX',
			'BudgetItem',
			'MapBudget',
			].indexOf(item.name) != -1){
			item.disabled = true // 设置字段disabled
		}
		return item
	})
}

//给相关字段赋值
function fz(obj,item,formData,list){
	formData.cCusName = obj.cCusName
	formData.cCusCode = obj.cCusCode
	formData.comCode1 = obj.comCode1
	let user = getUserInfo()
	let params = {
		paramasMap: {
			cCusCode: obj.cCusCode,
			comCode: ""
		},
		headersMap: {
			sysid: "bpm",
			syspwd: "pass",
			userid: user.bpmUid
		},
		url: bpmUrl + '/rest/appform/getDepartmentByCondition'
	}
	// // 省份
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: params,
		success:res => {
			formData.Department = res.data.data.Department
		}
	})
	// 客户类别
	params.url = bpmUrl + '/rest/appform/getcCusDefine6'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: params,
		success:res => {
			formData.cCusDefine6 = res.data.data.cCusDefine6
		}
	})
	//传真号码(cCusFax)
	params.url = bpmUrl + '/rest/appform/getcCusFax'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: params,
		success:res => {
			formData.cCusFax = res.data.data.cCusFax
		}
	})
	//电话(cCusPhone)
	params.url = bpmUrl + '/rest/appform/getcCusPhone'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: params,
		success:res => {
			formData.cCusPhone = res.data.data.cCusPhone
		}
	})
	//手机(cCusHand)
	params.url = bpmUrl + '/rest/appform/getcCusHand'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: params,
		success:res => {
			formData.cCusHand = res.data.data.cCusHand
		}
	})
	//客户类型(Cust_TYPE)
	params.url = bpmUrl + '/rest/appform/getcCusHand'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: params,
		success:res => {
			formData.cCusHand = res.data.data.cCusHand
		}
	})
	// formData.Cust_TYPE = '客户类型'
	//开票类型(cCusDefine2)
	params.url = bpmUrl + '/rest/appform/getcCusDefine2'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: params,
		success:res => {
			formData.cCusDefine2 = res.data.data.cCusDefine2
		}
	})
	//合作情况(ccdefine7)
	params.url = bpmUrl + '/rest/appform/getccdefine7'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: params,
		success:res => {
			formData.ccdefine7 = res.data.data.ccdefine7
		}
	})
	
	//根据被授权医院名称返回其所对应的大区
	params.url = bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B105'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: {
			paramasMap:{
				cCusCode:formData.cCusCode,
				OldDepart:formData.OldDepart
			}
		},
		success:res => {
			formData.OldDepart = res.data.data.OldDepart
		}
	})
	
	list.map(item => {
		if(['comCode1',
			'cCusCode',
			'Department',
			'cCusFax',
			'cCusPhone',
			'cCusHand',
			'cCusDefine2',
			'ccdefine7',
			].indexOf(item.name) != -1){
			item.disabled = true // 设置字段disabled
		}
		return item
	})
}

function formonsubmit(list,formData,bussiness){
	let flag = true
	let currentNodeid = bussiness.currentNodeid
	if(currentNodeid == "T10002"){
		//验证客户编码是否存在
		var cCusCode = formData.cCusCode;
		if(cCusCode==""){
			uni.showToast({
				title: "【请填写客户编码！】",
				icon:'none'
			})
			return false;
		}
		//验证客户编码是否存在
		var cCusName = formData.cCusName;
		if(cCusName==0){
			uni.showToast({
				title:"【客户名称和编码有误，请检查后重新输入！】",
				icon:'none'
			})
			return false;
		}         
		//验证数量字段不能为空
		var docunid = bussiness.docUnid;
		let user = getUserInfo()
		let paramas = {
			paramasMap: {
				docunid:docunid
			},
			headersMap: {
				sysid: "bpm",
				syspwd: "pass",
				userid: user.bpmUid
			},
			url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B120'
		}
		ajax({
			url:'/bpm/get',
			method: 'POST',
			data: paramas,
			success:res => {
				if(res.data.data= 1){
				//alert("【请填写表体中数量！】");
				//return false;
				}
			}
		})
		//验证子表信息不为空
		paramas.url = bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B102'
		ajax({
			url:'/bpm/get',
			method: 'POST',
			data: paramas,
			success:res => {
				if(res.data.data= 1){
				//alert("【请填写表体中数量！】");
				//return false;
				}
			}
		})
		
		//验证预算名称不能为空
		let BudgetCode = formData.BudgetCode
		if(BudgetCode==""){
			uni.showToast({
				title:"请选择预算名称",
				icon:'none'
			})
			return false;
		}
		
		//判断授权客户+产品列表不能为空
		// if(){
		// 	uni.showToast({
		// 		title: "【请填写表体信息！】",
					// icon:'none'
		// 	})
		// 	return false;
		// }
		//验证产品和授权有效期不能为空
		// alert("【请填写【授权客户+产品列表】中的产品信息！】");
		// alert("【请填写【授权客户+产品列表】中的授权有效期！】");
		
	}
	list = list.map(item => {
		if(['cCusCode','comCode1','Department','cCusDefine6','cCusFax',
		'cCusPhone','cCusHand','Cust_TYPE','cCusDefine7','cCusDefine2',
		'BudgetName','CBZX','BudgetItem'].indexOf(item.name) != -1){
			item.disabled = false
		}
		return item
	})
	return flag;
}

//模糊搜索客户列表
function getCustomerName(queryString,formData, cb) {
	console.log(queryString, cb)
	let user = getUserInfo()
	let paramas = {
		paramasMap: {
			cCusName: queryString
		},
		headersMap: {
			sysid: "bpm",
			syspwd: "pass",
			userid: user.bpmUid
		},
		url: bpmUrl + '/rest/appform/getCustomerInfo'
	}
	ajax({
		url: '/bpm/get',
		method: 'POST',
		data: paramas,
		success: res => {
			console.log(res)
			let data = res.data.data.map(item => {
				item.id = item.cCusCode
				item.name = item.cCusName
				return item
			})
			console.log(data)
			cb(data)
		}
	})
}

//模糊匹配预算名称
function getItem(queryString,formData, cb) {
	console.log(queryString, cb)
	let user = getUserInfo()
	let paramas = {
		paramasMap: {
			BudgetCode: queryString
		},
		headersMap: {
			sysid: "bpm",
			syspwd: "pass",
			userid: user.bpmUid
		},
		url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B074'
	}
	ajax({
		url: '/bpm/get',
		method: 'POST',
		data: paramas,
		success: res => {
			console.log(res)
			let data = res.data.data.map(item => {
				item.id = item.BudgetCode
				item.name = item.BudgetName
				return item
			})
			console.log(data)
			cb(data)
		}
	})
}

//给预算号相关字段再次赋值
function fznext1(formData){
	let BudgetCode = formData.BudgetCode
	let BudgetName = formData.BudgetName
	let paramas = {
		paramasMap: {
			BudgetCode:BudgetCode,
			BudgetName:BudgetName
		},
		headersMap: {
			sysid: "bpm",
			syspwd: "pass",
			userid: user.bpmUid
		},
		//"rule?wf_num=R_CRMVR_B123&BudgetCode="+BudgetCode+"" 接口文档无此接口
		url: bpmUrl + '/rest/appform/appform/R_appform_CRMVR_B123'
	}
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: paramas,
		success:res => {
			formData.BudgetName = res.data.data
		}
	})
	//成本中心赋值
	let CBZX = formData.CBZX
	paramas = {
		paramasMap: {
			BudgetCode:BudgetCode,
			BudgetName:BudgetName,
			CBZX:CBZX
		},
		headersMap: {
			sysid: "bpm",
			syspwd: "pass",
			userid: user.bpmUid
		},
		//"rule?wf_num=R_CRMVR_B117&BudgetName="+BudgetName+"&BudgetCode="+BudgetCode+"" 接口文档无此接口
		url: bpmUrl + '/rest/appform/appform/R_appform_CRMVR_B117'
	}
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: paramas,
		success:res => {
			formData.CBZX = res.data.data
		}
	})
	
	//预算项目赋值
	let BudgetItem = formData.BudgetItem
	paramas = {
		paramasMap: {
			BudgetCode:BudgetCode,
			BudgetName:BudgetName,
			BudgetItem:BudgetItem
		},
		headersMap: {
			sysid: "bpm",
			syspwd: "pass",
			userid: user.bpmUid
		},
		//"rule?wf_num=R_CRMVR_B118&BudgetName="+BudgetName+"&BudgetCode="+BudgetCode+"" 接口文档无此接口
		url: bpmUrl + '/rest/appform/appform/R_appform_CRMVR_B118'
	}
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: paramas,
		success:res => {
			formData.BudgetItem = res.data.data
		}
	})
	//预算号对应预算项目
	let MapBudget = formData.MapBudget
	paramas = {
		paramasMap: {
			BudgetCode:BudgetCode,
			BudgetName:BudgetName,
			MapBudget:MapBudget
		},
		headersMap: {
			sysid: "bpm",
			syspwd: "pass",
			userid: user.bpmUid
		},
		//"rule?wf_num=R_CRMVR_B119&BudgetName="+BudgetName+"&BudgetCode="+BudgetCode+"" 接口文档无此接口
		url: bpmUrl + '/rest/appform/appform/R_appform_CRMVR_B119'
	}
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: paramas,
		success:res => {
			formData.MapBudget = res.data.data
		}
	})
	
}

export default {
	formonload,
	fz,
	fz1,
	getItem,
	formonsubmit,
	getCustomerName
}